package com.lcj.crawler;

import com.geccocrawler.gecco.pipeline.Pipeline;
import com.geccocrawler.gecco.request.HttpGetRequest;
import com.geccocrawler.gecco.request.HttpRequest;
import com.geccocrawler.gecco.scheduler.DeriveSchedulerContext;
import com.lcj.model.CompanyRecruitInfo;
import com.lcj.service.CompanyRecruitInfoService;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SaveZptPipeline implements Pipeline<ZptIndexPage> {

    @Resource(name="companyRecruitInfoServiceImpl")
    private CompanyRecruitInfoService companyRecruitInfoService;
    @Override
    public void process(ZptIndexPage zptIndexPage) {
        List<ZptCompanyRecruit> list = zptIndexPage.getZptCompanyRecruits();
        for(ZptCompanyRecruit zptCompanyRecruit:list){
            CompanyRecruitInfo companyRecruitInfo = new CompanyRecruitInfo();
            if(zptCompanyRecruit.getRecruitName2()==null){
                zptCompanyRecruit.setRecruitName2("");
            }
            companyRecruitInfo.setRecruitName(zptCompanyRecruit.getRecruitName()+zptCompanyRecruit.getRecruitName2());
            companyRecruitInfo.setCompanyName(zptCompanyRecruit.getCompanyName());
            companyRecruitInfo.setRecruitUrl(zptCompanyRecruit.getRecruitUrl());
            companyRecruitInfo.setWorkPlace(zptCompanyRecruit.getAddress());
            companyRecruitInfo.setSalary(zptCompanyRecruit.getSalary());
            companyRecruitInfo.setSource("红星人才网招聘通");
            companyRecruitInfoService.saveZpt(companyRecruitInfo);

            //进入公司链接页面爬取联系方式等信息
            //https://www.zptong.com/jobs/jobs-show-98729-.htm
            String recruitId = "";
            recruitId = zptCompanyRecruit.getRecruitUrl().split("-")[2];
            String ajaxContactUrl = "https://www.zptong.com/plus/ajax_contact.php?id="+recruitId+"&time="+System.currentTimeMillis()+"&act=jobs_contact";
            HttpGetRequest sub = new HttpGetRequest(zptCompanyRecruit.getRecruitUrl());
            HttpGetRequest sub2 = new HttpGetRequest(ajaxContactUrl);
            DeriveSchedulerContext.into(sub);
            DeriveSchedulerContext.into(sub2);
        }

        String pageAll = zptIndexPage.getPageAll();
        String totalPageStr = StringUtils.substringBefore(StringUtils.substringAfter(pageAll,"/"),"页");
        String currentPageStr = StringUtils.substringBefore(pageAll,"/");
        int totalPage = getPageInt(totalPageStr);
        int currentPage = getPageInt(currentPageStr);
        if(currentPage<totalPage){
            int nextPage = currentPage+1;
            String nextUrl = "https://www.zptong.com/jobs/jobs-list.php?page="+nextPage;
            HttpRequest request = zptIndexPage.getRequest().subRequest(nextUrl);
            DeriveSchedulerContext.into(request);
        }
    }

    private int getPageInt(String totalPageStr) {
        if(StringUtils.isEmpty(totalPageStr)) {
            return 1;
        }
        return NumberUtils.toInt(totalPageStr, 1);
    }
}
